package com.deke.course.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.course.dao.EduCourseCheckDao;
import com.deke.course.dao.EduCourseDao;
import com.deke.course.dao.EduCourseDescriptionDao;
import com.deke.course.dao.EduSubjectDao;
import com.deke.course.service.EduCourseService;
import com.deke.course.vo.ClassData;
import com.deke.course.vo.CourseVo;
import com.deke.course.vo.MyListPage;
import com.deke.course.vo.QianSubjectVo;
import com.deke.entity.EduCourse;
import com.deke.entity.EduCourseDescription;
import com.deke.entity.EduSubject;
import com.deke.vo.Result;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author :LiM
 * @Date :2022/12/10 0:12
 */
@Service
public class EduCourseServiceImpl implements EduCourseService {
    @Autowired
    private EduCourseDao courseDao;
    @Autowired
    private EduSubjectDao eduSubjectDao;
    @Autowired
    private EduCourseCheckDao courseCheckDao;
    @Autowired
    private EduCourseDescriptionDao descriptionDao;


    /**
     * 分页查询
     * @Return
     * @Author Lim
     * @Date 2022/12/20 13:04
     */
    @Override
    public Result<IPage<EduCourse>> findByConditionPage(CourseVo courseVo, Integer current, Integer pageSize) {
        IPage<EduCourse> page = new Page(current,pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(courseVo.getTitle())){
            wrapper.like("title",courseVo.getTitle());
        }
        if (StringUtils.hasText(courseVo.getStatus())){
            wrapper.eq("status",courseVo.getStatus());
        }

        IPage<EduCourse> ipage =courseDao.selectPage(page, wrapper);
        for (EduCourse record : ipage.getRecords()) {
                String subjectName = eduSubjectDao.selectSubjectById(record.getSubjectId());
                record.setSubjectName(subjectName);
            String description = descriptionDao.selectdescriptionById(record.getId());
            record.setDescription(description);
            if(!record.getSubjectParentId().equals("0")){
                String subjectName2 = eduSubjectDao.selectSubjectById(record.getSubjectParentId());
                String quan=subjectName2+">"+subjectName;
                record.setSubjectName(quan);
            }
        }
        return new Result(200,"查询成功",ipage);
    }
    /**
     * 根据id修改状态
     * @Return
     * @Author Lim
     * @Date 2022/12/20 13:04
     */
    @Override
    public Result changeStatus(Map map) {
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(map.get("id").toString());
        eduCourse.setStatus(map.get("status").toString());
        int update=courseDao.updateById(eduCourse);
        if(update>0){
            return new Result(200,"修改上下架成功");
        }
        return new Result(000,"修改状态成功失败");
    }

    /**
     * 修改功能
     * @Return
     * @Author Lim
     * @Date 2022/12/20 13:04
     */
    @Override
    public Result updateCourse(CourseVo courseVo) {
        EduCourse course=new EduCourse();
        course.setId(courseVo.getId());
        course.setTitle(courseVo.getTitle());
        course.setPrice(courseVo.getPrice());
        int i = courseDao.updateById(course);
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseVo.getId());
        description.setDescription(courseVo.getDescription());
        int a = descriptionDao.updateById(description);
        if(i>0&&a>0){
        return new Result(200,"修改成功");
        }
        return new Result(000,"修改失败");
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 前台  课程模糊查询 
     * @create time: 2022/12/21 22:36
     * @param null
     * @return :  
     **/
    @Override
    public Result<IPage<EduCourse>> fontFindCourse(Integer pageCurrent, Integer pageSize, QianSubjectVo qianSubjectVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(qianSubjectVo.getIsFee())){
            if (qianSubjectVo.getIsFee().equals("0")){
                wrapper.eq("price",0);
            }else {
                wrapper.gt("price",0);
            }
        }
        if (StringUtils.hasText(qianSubjectVo.getOneSubjectId())){
            wrapper.eq("subject_parent_id",qianSubjectVo.getOneSubjectId());
        }
        if (StringUtils.hasText(qianSubjectVo.getTwoSubjectId())){
            wrapper.eq("subject_id",qianSubjectVo.getTwoSubjectId());
        }
        List<EduCourse> courseList = courseDao.selectList(wrapper);
        System.out.println(courseList.size());
        com.deke.course.vo.Page page = MyListPage.startPage(courseList, pageCurrent, pageSize);
        return new Result(2000,"成功",page);
    }

    @Override
    public Result queryCourseShoucangByMemId(String memberId) {
        List<EduCourse> eduCourses = this.courseDao.queryCourseShoucangByMemId(memberId);
        if(ObjectUtil.isNotNull(eduCourses)){
            return new Result(2000,"查询成功",eduCourses);
        }
        return new Result(0000,"查询失败",null);
    }

    @Override
    public Result queryCourseAndTeacherByCourseId(String courseId) {
        ClassData classData = courseDao.queryCourseAndTeacherByCourseId(courseId);
        if(ObjectUtil.isNotNull(classData)){
            return new Result(2000,"查询成功",classData);
        }
        return new Result(0000,"查询失败",null);
    }
    /**
     * 添加课程收藏
     * @param eduCourse
     * @return
     */
    @Override
    public Result addCollectCourse(EduCourse eduCourse) {
        Integer i = courseDao.addCollectCourse(eduCourse);
        if(i>0){
            return new Result(2000,"收藏成功",1);
        }
        return new Result(0000,"查询失败",null);
    }
    /**
     * 取消课程收藏
     * @param eduCourse
     * @return
     */
    @Override
    public Result deleteFavoriteCourse(EduCourse eduCourse) {
        Integer i = courseDao.deleteFavoriteCourse(eduCourse);
        if(i>0){
            return new Result(2000,"取消收藏成功",0);
        }
        return new Result(0000,"取消收藏失败",null);
    }
    /**
     * 渲染页面是否收藏了已收藏是1未收藏是0
     * @param
     * @return
     */
    @Override
    public Result findFavoriteCourse(EduCourse course) {
        Integer i = courseDao.findFavoriteCourse(course);
        if (i > 0) {
            return new Result(2000, "已收藏", 1);
        }else {
            return new Result(2000, "未收藏", 0);
        }
    }
}
